package com.wn55.hrms.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn55.hrms.entity.Employee;
import com.wn55.hrms.entity.Salary;
import com.wn55.hrms.entity.Security;
import com.wn55.hrms.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.SimpleTimeZone;

/**
 * @author Administrator
 */
@Service
public class SalaryService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private SalaryMapper salaryMapper;
    @Autowired
    private SecurityMapper securityMapper;
    @Autowired
    private AttendanceMapper attendanceMapper;
    @Autowired
    private OverTimeMapper overTimeMapper;
    @Autowired
    private EvectionMapper evectionMapper;
    @Autowired
    private LeaveMapper leaveMapper;

    public PageInfo<Salary> listByCondition(Integer currentPage, Integer pageSize, String name) {
        PageHelper.startPage(currentPage, pageSize);
        List<Salary> list = salaryMapper.listByCondition(name);
        return new PageInfo<>(list);
    }

    @Transactional
    public void insertSalary(Integer id, String salaryMonth) throws ParseException {
        Employee employee=employeeMapper.selectByPrimaryKey(id.longValue());
        Salary salary = new Salary();
        //根据员工id查询该员工在五险一金表里面的数据
        Security security = securityMapper.selectSecurityByEmpId(employee.getId());
        salary.setsId(security.getsId());
        salary.setEmpCode(employee.getId().intValue());
        salary.setUname(employee.getEmpName());
        salary.setSalaryMonth(new SimpleDateFormat("yyyy-MM").parse(salaryMonth));
        salaryMonth=salaryMonth.replace("-","_");
        System.out.println(salaryMonth);
        salary.setuBasicSalary(employee.getSalary());
        //根据时间模糊查询该月的所有考勤记录,计算全勤奖
        int day = attendanceMapper.selectAllDay(salaryMonth, employee.getId());
        if (day > 21) {
            salary.setPerfectAttendance(200.0);
        } else {
            salary.setPerfectAttendance(0.0);
        }
        salary.setBonus(0.0);
        salary.setSubsidies(100.0);
        /*
        注意：查询需要返回一个int,但是当数据不存在时为null,产生数据类型不匹配错误，使用ifnull解决
         */
        //加班时长
        int time = overTimeMapper.selectOvertimeByMonth(salaryMonth, employee.getId());
        salary.setOvertimePay((double) (time / 36000) * 20);
        //出差时长
        int ETime = evectionMapper.selectEvectionByMonth(salaryMonth, employee.getId());
        salary.setEvectionPay((double) (ETime / (60 * 60 * 24*10)) * 100);
        //请假时长
        int LTime = leaveMapper.selectLeaveByMonth(salaryMonth, employee.getId());
        salary.setLeavePay((double) (LTime / (60 * 60*10)) * (employee.getSalary() / (21 * 8)));
        salary.setSendAmount(salary.getuBasicSalary() + salary.getPerfectAttendance() + salary.getOvertimePay() + salary.getEvectionPay() - salary.getLeavePay());

        //个人三险一金
        salary.setEndowmentInsurance(salary.getuBasicSalary() * security.getEndowmentInsurance());
        salary.setMedicalInsurance(salary.getuBasicSalary() * security.getMedicalInsurance());
        salary.setUnemploymentInsurance(salary.getuBasicSalary() * security.getUnemploymentInsurance());
        salary.setAccumulationFund(salary.getuBasicSalary() * security.getAccumulationFund());

        double ins=salary.getEndowmentInsurance() + salary.getMedicalInsurance() + salary.getUnemploymentInsurance() + salary.getAccumulationFund();
   /*     税率
        1.不超过3000元的税率为：3%  速算扣除数： 0
        2. 超过3000元至12000元的部分税率为： 10%      速算扣除数：210
        3. 超过12000元至25000元的部分税率为： 20%    速算扣除数：1410
        4. 超过25000元至35000元的部分税率为： 25%    速算扣除数：2660
        5.超过35000元至55000元的部分税率为： 30%     速算扣除数：4410
        6.超过55000元至80000元的部分税率为： 35%     速算扣除数：7160
        7.超过80000元的部分税率为： 45%              速算扣除数：15160
        应纳税所得额=(月收入-五险一金-起征点-依法确定的其他扣除-专项附加扣除)*适用税率-速算扣除数*/
        double money = salary.getSendAmount() - 5000 - ins;
        double tax=0.0;
        if (money > 0 && money <= 3000) {
            tax = money * 0.03;
        } else if (money > 300 && money <= 12000) {
            tax = money * 0.1 - 210;
        } else if (money > 12000 && money <= 25000) {
            tax = money * 0.2 - 1410;
        } else if (money > 25000 && money <= 35000) {
            tax = money * 0.25 - 2660;
        }else if (money > 35000 && money <= 55000) {
            tax = money * 0.3 - 4410;
        } else if (money > 55000 && money <= 80000) {
            tax = money * 0.35 - 7160;
        } else if (money > 80000) {
            tax = money * 0.45 - 15160;
        }
        salary.setIncomeTax(tax);
        salary.setNetAmount(salary.getSendAmount()-tax-ins);
        salaryMapper.insert(salary);
    }


    public void removeByKey(Integer id) {
        salaryMapper.deleteByKey(id);
    }
}
